package com.cxx.rabbit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxx.rabbit.enums.OrderStatus;
import com.cxx.rabbit.mapper.CourseMapper;
import com.cxx.rabbit.service.OrderService;
import com.cxx.rabbit.utils.IdUtil;
import com.cxx.rabbit.domain.Order;
import com.cxx.rabbit.mapper.OrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public boolean saveOrder(Order order) {

        order.setOrderNo(IdUtil.genOrderNo());
        Date date = new Date();
        order.setCreateTime(date);
        order.setUpdateTime(date);
        order.setOrderStatus(OrderStatus.CREATE.getCode());

        orderMapper.insert(order);

        rabbitTemplate.send("e.ttl", "r.ttl", new Message(order.getOrderNo().getBytes(), MessagePropertiesBuilder.newInstance().build()));

        return true;
    }

    @Override
    @Transactional
    public boolean cancelOrder(Order order) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", order.getOrderNo());
        Order orderDB = orderMapper.selectOne(wrapper);

        if (orderDB == null || !Objects.equals(orderDB.getOrderStatus(), OrderStatus.CREATE.getCode())) {
            System.err.println("无需更新");
            return false;
        }

        orderDB.setOrderStatus(OrderStatus.CANCEL.getCode());
        orderDB.setUpdateTime(new Date());
        orderMapper.updateById(orderDB);
        return true;
    }

    @Override
    @Transactional
    public boolean payOrder(Order order) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        Message receive = rabbitTemplate.receive("q.ttl");
        if (receive == null) {
            return false;
        }
        byte[] body = receive.getBody();

        System.out.println("ttl队列取出消息" + new String(body));
        wrapper.eq("orderNo", order.getOrderNo());
        Order orderDb = orderMapper.selectOne(wrapper);
        if (Objects.equals(orderDb.getOrderStatus(), OrderStatus.CANCEL.getCode())) {
            return false;
        }
        orderDb.setOrderStatus(OrderStatus.PAYED.getCode());
        orderDb.setUpdateTime(new Date());
        orderMapper.updateById(orderDb);

        return true;
    }

    @Override
    public List<Order> listOrders(Integer userId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return orderMapper.selectList(wrapper);
    }

    @Override
    public Order getByOrderNo(String orderNo) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", orderNo);
        return orderMapper.selectOne(wrapper);
    }

}
